package com.carefree.system.modules.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carefree.common.core.PageUtils;
import com.carefree.common.core.QueryParam;
import com.carefree.system.modules.admin.dao.PostDao;
import com.carefree.system.modules.admin.entity.PostEntity;
import com.carefree.system.modules.admin.service.PostService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;


/**
 * 岗位
 *
 * @author tengbin
 */
@Service("postService")
public class PostServiceImpl extends ServiceImpl<PostDao, PostEntity> implements PostService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PostEntity> queryWrapper = new QueryWrapper<>();

        String keyword = (String) params.get("keyword");
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like("name", keyword).or().like("post_code", keyword);
        }
        IPage<PostEntity> page = page(new QueryParam<PostEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }


    /**
     * 根据id启用或禁用
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public boolean activeOrInActiveById(Long id, Integer status) {
        if (Objects.isNull(id) || Objects.isNull(status)) {
            return false;
        }
        PostEntity postEntity = getById(id);
        if (Objects.nonNull(postEntity)) {
            postEntity.setStatus(status);
            return updateById(postEntity);
        }
        return false;
    }
}
